#ifndef FFMPEG_MOVIEFRAME_H
#define FFMPEG_MOVIEFRAME_H

#include <unistd.h>
#include <global.h>

extern "C" {
#include "libavcodec/avcodec.h"
}

enum FrameType {
	MovieFrameTypeAudio, MovieFrameTypeVideo, MovieFrameNotDefine
};

enum Operation {
	FrameOperation_None,
	FrameOperation_Slap,
	FrameOperation_Scope,
	FrameOperation_SlapAndScope
};

class ScopeFrame {
public:
	AVPacket* packet;
	AVFrame* frame;
	FrameType type;
	Operation operation;

	ScopeFrame() {
		operation = FrameOperation_None;
		type = MovieFrameNotDefine;
		packet = NULL;
		frame = NULL;

	}

	~ScopeFrame() {
		if (packet != NULL) {
			av_free_packet(packet);
			delete packet;
		}

	}
	void setPacket(AVPacket* newPacket) {
		if (packet != NULL && packet != newPacket) {
			av_free_packet(packet);
			delete packet;
		}
		packet = newPacket;
	}

	void setFrame(AVFrame* newFrame) {
		if (frame != NULL && frame != newFrame) {
			av_frame_free(&frame);
			delete frame;
		}
		frame = newFrame;
	}

};

class MovieFrame {
public:
	float position;
	float duration;
	FrameType type;
	Operation operation;
	AVFrame* frame;

	MovieFrame() {
		position = 0.0f;
		duration = 0.0f;
		operation = FrameOperation_None;
		type = MovieFrameNotDefine;
		frame = NULL;
	}
	~MovieFrame() {
		if (frame != NULL) {
			av_frame_free(&frame);
			frame = NULL;
		}
	}

	void setAvFrame(AVFrame* newAvFrame) {
		if (frame != newAvFrame) {
			if (frame != NULL)
				av_frame_free(&frame);

			frame = newAvFrame;
		}
	}
};

class VideoFrame: public MovieFrame {
public:
	int width;
	int height;
	void* param;
	int paramLength;

	VideoFrame() {
		paramLength = 0;
		param = NULL;
	}

	~VideoFrame() {
		if (paramLength != 0) {
			delete[] (param);
			param = NULL;
			paramLength = 0;
		}
	}

	void setParam(void * data, int length) {
		if (length != paramLength) {
			if (paramLength != 0) {
				delete[] (param);
				paramLength = 0;
			}

			paramLength = length;
			param = new unsigned char[length];
		}

		memcpy(param, data, length);
	}
};

#endif // FFMPEG_MOVIEFRAME_H
